package cn.iocoder.yudao.module.design.dal.mysql.diary;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.design.controller.admin.diary.vo.DiaryExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.diary.vo.DiaryPageReqVO;
import cn.iocoder.yudao.module.design.controller.admin.diary.vo.DiaryRespVO;
import cn.iocoder.yudao.module.design.dal.dataobject.diary.DiaryDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 日记 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface DiaryMapper extends BaseMapperX<DiaryDO> {

    default PageResult<DiaryDO> selectPage(DiaryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DiaryDO>()
                .betweenIfPresent(DiaryDO::getDate, reqVO.getDate())
                .eqIfPresent(DiaryDO::getHouseId, reqVO.getHouseId())
                .eqIfPresent(DiaryDO::getNodeId, reqVO.getNodeId())
                .eqIfPresent(DiaryDO::getComment, reqVO.getComment())
                .eqIfPresent(DiaryDO::getCreator, reqVO.getCreator())
                .betweenIfPresent(DiaryDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(DiaryDO::getCreateTime));
    }

    default List<DiaryDO> selectList(DiaryExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DiaryDO>()
                .betweenIfPresent(DiaryDO::getDate, reqVO.getDate())
                .eqIfPresent(DiaryDO::getHouseId, reqVO.getHouseId())
                .eqIfPresent(DiaryDO::getNodeId, reqVO.getNodeId())
                .eqIfPresent(DiaryDO::getComment, reqVO.getComment())
                .betweenIfPresent(DiaryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DiaryDO::getId));
    }

        @Select("select hd.id ,hd.date,hd.house_id,hd.node_id,hd.comment  ,su.nickname  as createName \n" +
                ",hd.creator,hd.create_time as createTime       \n" +
                ",hd.updater,hd.update_time  as createTime , \n" +
                "hd.flow_id as flowId ,hh.project_name as projectName,hf.flow_name as flowName ,hn.node_name as nodeName   " +
                " from hd_diary hd ,hd_house hh ,hd_flow hf ,hd_node hn  ,system_users su \n" +
                "where hd.house_id =hh.id  and hd.flow_id =hf.id and  hd.node_id  = hn.id and hd.creator = su.id and hd.house_id  = #{houseId} order by hd.date,hd.create_time desc   limit #{startIndex},#{endIndex}")
          List<DiaryRespVO>  selectDiaryInfo(@Param("houseId") Long houseId, @Param("nodeId") Long nodeId,@Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex) ;
}
